#include <iostream>
#include <string>
#include <list>

int m;
std::string s;
std::string clip;
bool copy_status;
bool insert_mode;
int pos; // caret
int cp_s_pos, cp_e_pos;
int tot;

std::list<char> ans;
typedef std::list<char>::iterator iter_type;

iter_type caret;
iter_type cp_s_it;
iter_type cp_e_it;

void paste_to_clip(iter_type its, iter_type ite, int ps, int pe)
{
	clip = "";
	if (ps < pe) {
		for (auto it = its; it != ite; ++it)
			clip += *it;
	} else {
		for (auto it = ite; it != its; ++it)
			clip += *it;
	}
}

void delete_selection(iter_type its, iter_type ite, int ps, int pe)
{
	if (ps < pe) {
		for (auto it = its; it != ite; ) {
			it = ans.erase(it);
			pos--;
		}
	} else {
		for (auto it = ite; it != its; ++it)
			it = ans.erase(it);
	}
}

void insert_clip_to_ans()
{
	for (int i = 0; i < (int)clip.size(); i++) {
		ans.insert(caret, clip[i]);
		pos++;
	}
}

void replace_clip_to_ans()
{
	for (int i = 0; i < (int)clip.size(); i++) {
		if (caret == ans.end()) ans.push_back('!');
		(*caret) = clip[i];
		pos++;
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T;
	std::cin >> T;
	while (T--) {
		std::cin >> m;
		std::cin >> s;

		copy_status = false;
		insert_mode = true;
		clip = "";
		tot = 0;
		ans.clear();
		//std::list<char>::iterator caret = ans.begin();
		caret = ans.begin();
		pos = 0;

		for (int i = 0; i < (int)s.size(); i++) {
			char ch = s[i];
			if (ch == 'L') {
				if (caret != ans.begin()) {
					--caret;
					pos--;
				}
				continue;
			}
			if (ch == 'R') {
				if (caret != ans.end()) {
					++caret;
					pos++;
				}
				continue;
			}
			if (ch == 'S') {
				insert_mode = !insert_mode;
				continue;
			}
			if (ch == 'D') {
				if (!copy_status) {
					if (caret != ans.end())
						caret = ans.erase(caret);
				} else {
					copy_status = !copy_status;
					delete_selection(cp_s_it, cp_e_it, cp_s_pos, cp_e_pos);
				}
				continue;
			}
			if (ch == 'B') {
				if (caret != ans.begin()) {
					caret = ans.erase(--caret);
					pos--;
				}
				continue;
			}
			if (ch == 'C') {
				if (copy_status) {
					copy_status = !copy_status;
					cp_e_it = caret;
					cp_e_pos = pos;
					paste_to_clip(cp_s_it, cp_e_it, cp_s_pos, cp_e_pos);
				} else {
					copy_status = !copy_status;
					cp_s_it = caret;
					cp_s_pos = pos;
				}
				continue;
			}
			if (ch == 'V') {
				if (clip.size()) {
					if (insert_mode) {
						if (int(ans.size() + clip.size()) <= m) {
							insert_clip_to_ans();
						}
					} else {
						if ((int)clip.size() + pos <= m) {
							replace_clip_to_ans();
						}
					}
				}
				continue;
			}

			if (copy_status) {
				copy_status = !copy_status;
				cp_e_it = caret;
				cp_e_pos = pos;
				if (insert_mode) {
					if ((int)ans.size() + 1 <= m) {
						caret = ans.insert(caret, ch);
						tot++;
						pos++;
					}
				} else {
					
				}
			} else {
				if (insert_mode) {
					if ((int)ans.size() + 1 <= m) {
						caret = ans.insert(caret, ch);
						tot++;
						pos++;
					}
				} else {
				}
			}
		}
	}
}

